<?php
// affectation de variables inexistantes en cli
$_SERVER['SERVER_NAME'] = 'php';

$rootPath = dirname(dirname(__FILE__));
$appPath  = $rootPath . DIRECTORY_SEPARATOR . 'application';
$confPath = $appPath  . DIRECTORY_SEPARATOR . 'config';

set_include_path(get_include_path() . 
PATH_SEPARATOR . $appPath . 
PATH_SEPARATOR . $rootPath . DIRECTORY_SEPARATOR . 'library' . 
PATH_SEPARATOR . $appPath  . DIRECTORY_SEPARATOR . 'models');

require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();

$configMain = new Zend_Config_Ini($confPath . DIRECTORY_SEPARATOR . 'config.ini', 'dev');
$configRoutes = new Zend_Config_Ini($confPath . DIRECTORY_SEPARATOR . 'routes.ini');
$configSession = new Zend_Config_Ini($confPath . DIRECTORY_SEPARATOR . 'session.ini', 'dev');

// ************************** LOG ******************************

$log = new Zend_Log($writer = new Zend_Log_Writer_Stream($appPath . $configMain->logfile));
$log->addPriority('USER', 8);
Zend_Registry::set('log', $log);

// ************************* SESSION ***********************************

Zend_Session::setOptions($configSession->toArray());
Zend_Session::setOptions(array('save_path' => $appPath . $configSession->save_path));

Zend_Registry::set('session', $session = new Zend_Session_Namespace($configSession->name));
 
// ************************** LOCALE ********************************

Zend_Locale::$compatibilityMode = false;
$locale = new Zend_Locale();
Zend_Registry::set('Zend_Locale', $locale);

// ************************** DATABASE *******************************

$db = Zend_Db::factory($configMain->database);
$db->query("SET NAMES 'UTF8'");
Zend_Db_Table_Abstract::setDefaultAdapter($db);

//************************** ACL *************************************

if (! isset($session->acl)) {
    $acl = new Zend_Acl();
    $acl->addRole(new Zend_Acl_Role('user'));
    $acl->add(new Zend_Acl_Resource('reservations'));
    $session->acl = $acl;
}

// ************************ PAGINATEUR *********************************

Zend_Paginator::setDefaultScrollingStyle('Sliding');
Zend_View_Helper_PaginationControl::setDefaultViewPartial('common/pagination_control.phtml');

// ************************ MVC ****************************************

$frontController = Zend_Controller_Front::getInstance();
$frontController->setControllerDirectory($appPath . '/controllers');
$frontController->setParam('debug', $configMain->debug);
$frontController->setParam('locale', $locale);
$frontController->setParam('config', $configMain);

Zend_Controller_Action_HelperBroker::addPrefix('Zfbook_Controller_ActionHelpers');

$router = $frontController->getRouter();
$router->addConfig($configRoutes, 'routes');

// **************************** LAYOUTS ***********************************

Zend_Layout::startMvc(array('layoutPath' => $appPath . '/views/layouts'));

// ************************* TRANSLATE ************************************

$translate = new Zend_Translate('gettext', $appPath . '/languages', null, array('scan' => Zend_Translate::LOCALE_FILENAME));
$langLocale = isset($session->lang) ? $session->lang : $locale;
$translate->setLocale($langLocale);
Zend_Registry::set('Zend_Translate', $translate);
Zend_Validate_Abstract::setDefaultTranslator($translate);

// ************************* FORM ******************************************

Zend_Form::setDefaultTranslator($translate);
